<?php
namespace app\admin\controller;
use think\Controller;
use app\admin\model\Admin;

class Pub extends Controller
{


    public function login()
    {
       if(request()->isAjax() || request()->isPost()) {
            $auth_code=input('auth_code','','trim');
            if(!$auth_code)$this->error('请输入验证码');
            if(!captcha_check($auth_code))$this->error('验证码不正确');

            //验证输入信息
            $data = $this->request->post();
            $result = $this->validate($data,'app\admin\validate\AdminValidate.login');
            if(true !== $result)$this->error($result);
            $account = $data['account'];
            $password = $data['password'];

            $object= new Admin();
            $res=$object->login($account,$password);
            if(!$res){
                $msg=$object->getError();
                $this->error($msg);
            }

           return $this->success('登录成功','/'.config('common.admin_login_file'));
       }

        self::checkLoginFile();
        $uid = is_login();
        if($uid)return $this->redirect('/'.get_admin_login_file());

        return $this->fetch();
    }


    //验证码
    public function verifyImg(){
        $config =    [
            //验证码字体大小
            'fontSize'    =>    30,
            //验证码位数
            'length'      =>    4,
            'imageW' => 290,
            'imageH' => 55,
            //是否画混淆曲线
            'useCurve' => false,
            //关闭验证码杂点
            'useNoise'    =>    true,
            'codeSet' => '0123456789',
        ];
        return captcha('',$config);
    }



    //退出登录
    public function logout(){
        $save_pre = config("common.admin_login_save_pre");
        cookie($save_pre.'_auth', null);
        cookie($save_pre.'_auth_sign', null);
        session($save_pre.'_auth', null);
        session($save_pre.'_auth_sign', null);
        session($save_pre.'_rules',null);  //当前账号拥有的权限ids
        session($save_pre.'_author',null); //当前账号已验证的权限
        session('menu_left_tree',null);
        session('__forward__',null);
        $this->success('退出成功','/'.config('common.admin_login_file').'/pub/login');
    }


    //检查后台登录地址
    private function checkLoginFile(){
        $res=$_SERVER['SCRIPT_NAME'];
        if($res!='/'.get_admin_login_file()){
            return $this->redirect('/');
        }
    }


    public function test(){
        return $this->request->InApp;
    }

}
